package com.sci.web.daoimpl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sci.web.bean.Enterprise_Base;
import com.sci.web.bean.UserRegister;
import com.sci.web.dao.UserRegisterDao;

public class UserRegisterDaoImpl extends SqlMapClientDaoSupport implements UserRegisterDao {

	@Override
	public boolean isUserExist(String name) {
		// TODO Auto-generated method stub
		int index = 0;
		index = (Integer) super.getSqlMapClientTemplate().queryForObject("register.isuserexist", name);
		if (index > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean isEnterpriseNameExist(String name) {
		// TODO Auto-generated method stub
		int index = 0;
		index = (Integer) super.getSqlMapClientTemplate().queryForObject("register.isenterprisenameexist", name);

		if (index > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean isEnterperisecorporatecodExist(String code) {
		// TODO Auto-generated method stub
		int index = 0;
		index = (Integer) super.getSqlMapClientTemplate().queryForObject("register.isenterprisecorporatecodeexist", code);
		if (index > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 保证该函数在一个事务中进行处理,以便将数据的连贯性进行回滚 处理
	 */
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	@Override
	public boolean addNewUserAndEnterpriseBase(UserRegister ur, Enterprise_Base eb) {
		// TODO Auto-generated method stub
		int userindex = -1;
		int enterpriseindex = -1;
		userindex = (Integer) super.getSqlMapClientTemplate().insert("register.addnewuser", ur);
		eb.setUser_id(userindex);
		enterpriseindex = (Integer) super.getSqlMapClientTemplate().insert("register.addnewenterprisebase", eb);
		if (userindex > 0 && enterpriseindex > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public String getUserChargeAndUserPhone(int city_id) {
		// TODO Auto-generated method stub
		String phone = null;
		phone = (String) super.getSqlMapClientTemplate().queryForObject("register.getuserandphone", city_id);
		if (phone != null) {
			return phone;
		} else {
			return "没有联系方式";
		}
	}

	@Override
	public boolean isEnterpriseNameExistForEdit(int userId, String name) {
		// TODO Auto-generated method stub
		int length = -1;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userid", userId);
		map.put("name", name);
		length = (Integer) super.getSqlMapClientTemplate().queryForObject("register.isenterprsisenameexistforedit", map);
		if (length > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean isEnterrprisecorporatecodeExistEdit(int userId, String name) {
		// TODO Auto-generated method stub isEnterrprisecorporatecodeExistEdit
		int length = -1;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userid", userId);
		map.put("name", name);
		length = (Integer) super.getSqlMapClientTemplate().queryForObject("register.isEnterrprisecorporatecodeExistEdit", map);
		if (length > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean updateEnterpriseInfo(Enterprise_Base eb) {
		// TODO Auto-generated method stub
		int index = -1;
		index = (Integer) super.getSqlMapClientTemplate().update("register.updateenterprisebase", eb);
		if (index > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean insertUpdateEnterpriseInfo(Enterprise_Base eb) {
		// TODO Auto-generated method stub
		int index = -1;
		index = (Integer) super.getSqlMapClientTemplate().insert("register.insertupdateenterprisedata", eb);
		if (index > 0) 
			return true;
		else
			return false;
	}
}
